@file:Suppress("EnumEntryName", "NonAsciiCharacters")

package com.zxy.supplier_system.server.utils

import org.apache.poi.ss.usermodel.BorderStyle
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.math.BigDecimal

enum class GoodsExcelField {
    条码,
    名称,
    零售价,
    库存,
    品牌,
    类别,
    厂商,
    箱规,
    单位,
    数量,
    总售价,
    备案编号,
    注册证编号,
    备注,
}

class GoodsExcelColumn<T>(
    val title: String,
    val getter: ((T) -> Any?),
) {
    companion object {
        fun fromField(field: GoodsExcelField): GoodsExcelColumn<GoodsRow> {
            return GoodsExcelColumn(
                field.name, when (field) {
                    GoodsExcelField.名称 -> GoodsRow::name
                    GoodsExcelField.品牌 -> GoodsRow::brand
                    GoodsExcelField.零售价 -> GoodsRow::price
                    GoodsExcelField.库存 -> GoodsRow::stock
                    GoodsExcelField.箱规 -> GoodsRow::specification
                    GoodsExcelField.单位 -> GoodsRow::unit
                    GoodsExcelField.条码 -> GoodsRow::barcode
                    GoodsExcelField.类别 -> GoodsRow::category
                    GoodsExcelField.厂商 -> GoodsRow::factory
                    GoodsExcelField.数量 -> GoodsRow::amount
                    GoodsExcelField.总售价 -> GoodsRow::totalPrice
                    GoodsExcelField.备案编号 -> GoodsRow::recordFilingNumber
                    GoodsExcelField.注册证编号 -> GoodsRow::registrationCertificateNumber
                    GoodsExcelField.备注 -> GoodsRow::note
                }
            )
        }
    }
}

data class GoodsRow(
    val name: String? = null,
    val brand: String? = null,
    val price: BigDecimal? = null,
    val stock: Int? = null,
    val specification: String? = null,
    val unit: String? = null,
    val barcode: String? = null,
    val category: String? = null,
    val factory: String? = null,
    val costPrice: BigDecimal? = null,
    val amount: Int? = null,
    val registrationCertificateNumber: String? = null,
    val recordFilingNumber:String? = null,
    val note:String? = null,
    val executiveStandard:String?=null,
) {
    val totalPrice: BigDecimal?
        get() {
            return if (price != null && amount != null) {
                price.multiply(BigDecimal(amount))
            } else {
                null
            }
        }

}

fun exportGoodsDataToWorkbook(fields: Array<GoodsExcelField>, data: List<GoodsRow>): XSSFWorkbook {
    return exportGoodsDataToWorkbook(fields.map { GoodsExcelColumn.fromField(it) }.toTypedArray(), data)
}

fun <T> exportGoodsDataToWorkbook(fields: Array<GoodsExcelColumn<T>>, data: List<T>): XSSFWorkbook {
    val workbook = XSSFWorkbook()
    val sheet = workbook.createSheet()
    sheet.createRow(0).setCellValues(fields.map { it.title })
    data.forEachIndexed { index, goods ->
        val row = sheet.createRow(index + 1)
        row.setCellValues(
            fields.map { goodsExcelColumn ->
                goodsExcelColumn.getter(goods)
            }
        )
    }
    for (rowIndex in 0..sheet.lastRowNum) {
        val style = workbook.createCellStyle()
        style.borderTop = BorderStyle.THIN;
        style.borderBottom = BorderStyle.THIN;
        style.borderLeft = BorderStyle.THIN;
        style.borderRight = BorderStyle.THIN;
        for (columnIndex in fields.indices) {


            sheet.getRow(rowIndex).getCell(columnIndex, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK).cellStyle = style
        }
    }

    autoColumnWidthForChineseChar(sheet, fields.size)
    return workbook
}